package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.entity.CycleCollection;
import com.sc.nft.enums.CycleCollectionEnum;
import com.sc.nft.mapper.CycleCollectionMapper;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

/**
 * 定时更新上架的藏品表(sc_cycle_collection)数据DAO
 *
 * @author Tangzehui
 * @since 2023-12-15 11:36:46
 * @description 
 */
@Slf4j
@Repository
public class CycleCollectionDao extends ServiceImpl<CycleCollectionMapper, CycleCollection> {

    public CycleCollection getByDataKey(String dataKey, CycleCollectionEnum type) {
        LambdaQueryWrapper<CycleCollection> query = new LambdaQueryWrapper<>();
        query.eq(CycleCollection::getDataKey, dataKey);
        query.eq(CycleCollection::getType, type.getCode());
        query.last("LIMIT 1");
        return  getOne(query);
    }

    public void updateByCollectionId(CycleCollection cycleCollection){
        LambdaUpdateWrapper<CycleCollection> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CycleCollection::getDataKey, cycleCollection.getDataKey());
        updateWrapper.eq(CycleCollection::getType, cycleCollection.getType());
        updateWrapper.set(CycleCollection::getDataValue, cycleCollection.getDataValue());
        updateWrapper.set(CycleCollection::getOrderByKey, cycleCollection.getOrderByKey());
        update(new CycleCollection(),updateWrapper);
    }

    public void deleteByType(CycleCollectionEnum type) {
        LambdaUpdateWrapper<CycleCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CycleCollection::getType,type);
        remove(wrapper);
    }
}